package com.arui.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.arui.yygh.hosp.repository.DepartmentRepository;
import com.arui.yygh.hosp.service.DepartmentService;
import com.arui.yygh.model.hosp.Department;
import com.arui.yygh.vo.hosp.DepartmentQueryVo;

import com.arui.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> paramMap) {
        String mapString = JSONObject.toJSONString(paramMap);
        Department department = JSONObject.parseObject(mapString,Department.class);
        //根据医院编号和科室编号进行查询
        Department departmentExist =departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        if (departmentExist!=null){
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }else{
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }

    }
    //查询科室接口
    @Override
    public Page findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        //创建Pageable对象
        Pageable pageable = PageRequest.of(page-1,limit);
        //创建Example对象，加入条件
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        Example<Department> example = Example.of(department,matcher);

        Page<Department> page1 = departmentRepository.findAll(example, pageable);
        return page1;
    }
    //删除科室接口
    @Override
    public void remove(String hoscode, String depcode) {
        //根据医院编号和科室编号查询出科室信息
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department!=null){
            departmentRepository.deleteById(department.getId());
        }
    }
    //根据医院id，查询科室列表
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        ArrayList<DepartmentVo> result = new ArrayList<>();
        Department departmentQuery = new Department();

        departmentQuery.setHoscode(hoscode);
        Example<Department> example = Example.of(departmentQuery);

        List<Department> list = departmentRepository.findAll(example);
        Map<String, List<Department>> departmentMap = list.stream().collect(Collectors.groupingBy(Department::getBigcode));

        for (Map.Entry<String,List<Department>> entry:departmentMap.entrySet()) {
            //获得大科室编号
            String bigcode = entry.getKey();

            List<Department> departments = entry.getValue();
            DepartmentVo departmentVo  = new DepartmentVo();

            departmentVo.setDepcode(bigcode);
            departmentVo.setDepname(departments.get(0).getBigname());

            List<DepartmentVo> children = new ArrayList<>();
            for (Department department:departments){
                DepartmentVo departmentVo1 = new DepartmentVo();
                departmentVo1.setDepname(department.getDepname());
                departmentVo1.setDepcode(department.getDepcode());
                children.add(departmentVo1);
            }
            departmentVo.setChildren(children);
            result.add(departmentVo);
        }
        return result;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department!=null){
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
}
